<template>
  <page>
    <template v-slot:header>
      <h2>
        <i class="icon iconfont">&#xe612;</i>
        <span>{{ $t("patch.sp_upgrade") }}</span>
      </h2>
    </template>
    <template v-slot:content>
      <ky-table
        ref="table"
        :getData="getAvailableHosts"
        :showSelect="showSelect"
        :search_data="search_data"
      >
        <template v-slot:table-action>
          <authorization-button
            class="kylin-item-button"
            :disabled="$refs.table && $refs.table.selectRow.ids.length == 0"
            @click="handleIssueUpdate"
            name="sp_upgrade:run"
          >
            {{ $t("system.upgrade") }}
          </authorization-button>
        </template>
        <template v-slot:table>
          <el-table-column prop="ip" :label="$t('system.ip_address')">
          </el-table-column>
          <el-table-column :label="$t('system.status')">
            <template slot-scope="scope">
              <span
                v-if="scope.row.agentStatus == '连接'"
                class="el-icon-success success"
              ></span>
              <span v-else class="el-icon-error error"></span>
              {{ scope.row.agentStatus }}
            </template>
          </el-table-column>
          <el-table-column
            prop="operatingSystem"
            :label="$t('patch.system_info')"
          >
          </el-table-column>
          <el-table-column prop="version" :label="$t('system.system_version')">
          </el-table-column>
          <el-table-column
            prop="architecture"
            :label="$t('system.architecture')"
          >
          </el-table-column>
          <el-table-column prop="installTime" :label="$t('create_time')">
          </el-table-column>
        </template>
      </ky-table>
    </template>
  </page>
</template>

<script>
import Page from "@/components/Page";
import kyTable from "@/components/KyTable";
import { getAvailableHosts, issueSpUpgrade } from "@/api/patch";
import AuthorizationButton from "@/components/AuthorizationButton";
export default {
  components: {
    Page,
    kyTable,
    AuthorizationButton,
  },
  data() {
    return {
      search_data: {
        ip: {
          name: "system.ip_address",
          type: "text",
        },
        agentStatus: {
          name: "system.status",
          type: "select",
          items: [
            { key: "连接", name: "连接" },
            { key: "断开", name: "断开" },
          ],
        },
        operatingSystem: {
          name: "patch.system_info",
          type: "select",
          items: [
            {
              key: "银河麒麟高级服务器操作系统",
              name: "银河麒麟高级服务器操作系统",
            },
            {
              key: "中标麒麟高级服务器操作系统",
              name: "中标麒麟高级服务器操作系统",
            },
          ],
        },
        version: {
          name: "system.version",
          type: "select",
          items: [
            { key: "V10(SP1)", name: "V10(SP1)" },
            { key: "V10(SP2)", name: "V10(SP2)" },
            { key: "V7", name: "V7" },
          ],
        },
        installTime: {
          name: "system.install_time",
          type: "date",
        },
        architecture: {
          name: "system.architecture",
          type: "select",
          items: [
            { key: "aarch64", name: "aarch64" },
            { key: "x86_64", name: "x86_64" },
          ],
        },
      },
      fileList: [],
      showSelect: true,
      tableData: [
        {
          success: 1,
          failed: 1,
          pending: 0,
          cancel: 0,
        },
      ],
    };
  },
  methods: {
    getAvailableHosts(data) {
      return getAvailableHosts(data, this.$route.query.upgradeCheckId);
    },
    issueSpUpgrade,
    handleIssueUpdate() {
      let ips = [];
      this.$refs.table.selectRow.rows.forEach((row) => {
        ips.push(row.ip);
      });
      this.$message({
        type: "success",
        message: "已下发升级请求，请稍候",
      });
      this.issueSpUpgrade({ id: this.$route.params.id, data: ips }).then(
        (res) => {
          if (res.data.code == 0) {
            this.$message({
              type: "success",
              message: "下发成功，升级执行中请稍候",
            });
            if (res.data.data && res.data.data.id) {
              this.$router.push({
                name: "log_detail",
                params: { id: res.data.data.id },
              });
            }
          } else {
            this.$message({
              type: "error",
              message: res.data.message,
            });
          }
        }
      );
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.register_title {
  margin-bottom: 10px;
}
.register-upload {
  height: 120px;
}
.success {
  color: #67c23a;
  font-size: 16px;
}
.error {
  color: red;
  font-size: 16px;
}
</style>